package com.topica;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.topica.network.SoapCaller;

public class TopicaLogin extends Activity {

	private static final String PREF_USER = "_prefuser";
	private EditText editUsername;
	private EditText editPassword;
	private CheckBox remember;
	private Button btnLogin;
	private ProgressDialog pDialog;
	private SharedPreferences prefUser;
	public static String USER_NAME_KEY = "_username";
	public static String USER_ID_KEY = "_userid";
	public static String PASSWORD_KEY = "_password";
	private String userName;
	private String userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		prefUser = getSharedPreferences(PREF_USER, Context.MODE_PRIVATE);
		userId = prefUser.getString(USER_ID_KEY, "");
		if(userId.equals("")){
			editPassword = (EditText) findViewById(R.id.edit_password);
			editUsername = (EditText) findViewById(R.id.edit_username);
			remember = (CheckBox) findViewById(R.id.chk_remember);
			btnLogin = (Button) findViewById(R.id.btn_login);

			btnLogin.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					LoginTask task = new LoginTask();
					task.execute();
				}
			});
		} else {
			userName = prefUser.getString(USER_NAME_KEY, "");
			((TopicaApplication) getApplication()).setUserId(userId);
			((TopicaApplication) getApplication()).setUsername(userName);
			Intent intent = new Intent(getApplicationContext(),TopicaCourse.class);
			startActivity(intent);
		}
	}

	private class LoginTask extends AsyncTask<String, Void, Integer> {
		private String userId;
		private final static int OK = 1;
		private final static int FAIL = 0;

		@Override
		protected Integer doInBackground(String... arg0) {
			PropertyInfo pi = new PropertyInfo();
			pi.setName("password");
			pi.setValue(editPassword.getText().toString());
			PropertyInfo pi1 = new PropertyInfo();
			pi1.setName("username");
			pi1.setValue(editUsername.getText().toString());
			List<PropertyInfo> pp = new ArrayList<PropertyInfo>();
			pp.add(pi1);
			pp.add(pi);
			SoapCaller caller = new SoapCaller();
			int rs = caller.call("checkLogin", pp);
			if (rs != 0) {
				userId = "" + rs;
				return OK;
			} else {
				return FAIL;
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			pDialog.dismiss();
			if (result == OK) {
				String username = editUsername.getText().toString();
				((TopicaApplication) getApplication()).setUserId(userId);
				((TopicaApplication) getApplication()).setUsername(username);
				Editor editor = prefUser.edit();
				editor.putString(USER_NAME_KEY, username);
				editor.putString(USER_ID_KEY, userId);
				editor.putString(PASSWORD_KEY, editPassword.getText().toString());
				editor.commit();
				Intent intent = new Intent(getApplicationContext(),TopicaCourse.class);
				startActivity(intent);
			} else {
				Toast.makeText(getApplicationContext(), "Login Fail",
						Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(TopicaLogin.this);
			pDialog.setMessage(getResources().getString(R.string.title_login));
			pDialog.setCancelable(false);
			pDialog.show();
		}

	}
}
